import _ from 'lodash'

/**
 * @param {Array} list
 * @param {Function} f
 * @return {*}
 */
function find (list, f) {
  return list.filter(f)[0]
}

/**
 * @param {*} obj
 * @param {Array<Object>} cache
 * @return {*}
 */
export function deepCopy (obj, cache = []) {
  // just return if obj is immutable value
  if (obj === null || typeof obj !== 'object') {
    return obj
  }
  // if obj is hit, it is in circular structure
  const hit = find(cache, c => c.original === obj)
  if (hit) {
    return hit.copy
  }
  const copy = Array.isArray(obj) ? [] : {}
  // put the copy into cache at first
  // because we want to refer it in recursive deepCopy
  cache.push({
    original: obj,
    copy
  })
  Object.keys(obj).forEach(key => {
    copy[key] = deepCopy(obj[key], cache)
  })
  return copy
}

/**
 * forEach for object
 */
export function forEachValue (obj, fn) {
  Object.keys(obj).forEach(key => fn(obj[key], key))
}

/**
 * delNullObject
 * @desc 删除object 中key 为null的值
 * @return 新对象
 */
export function delNullObject (obj) {
  let copy = deepCopy(obj)
  _.forOwn(copy, (val, key, obj) => {
    if (val == null) {
      delete obj[key]
    }
  })
  return copy
}

/**
 * delNullObject
 * @desc 删除object 中key 为 '' 的值
 * @return 新对象
 */
export function delEmptyStr (obj) {
  let copy = deepCopy(obj)
  _.forOwn(copy, (val, key, obj) => {
    if (val === '') {
      delete obj[key]
    }
  })
  return copy
}

/**
 * paramFilter
 * @desc 删除object 中key 为 === '' 或 == null 的值
 * @return 新对象
 */
export function paramFilter (obj) {
  let copy = deepCopy(obj)
  _.forOwn(copy, (val, key, obj) => {
    if (val === '' || val == null) {
      delete obj[key]
    }
  })
  return copy
}

/**
 * zeroToString
 * @desc 修改object 中 key 为 == '0' 的值为 ''
 * @return 修改后的 object
 */
export function zeroToString (obj, keys) {
  _.forOwn(obj, (val, key, obj) => {
    if (val + '' === '0' && _.includes(keys, key)) {
      obj[key] = ''
    }
  })
  return obj
}

/**
 * 获取字符串真实长度 汉字算两位
 * @param str
 * @returns {number}
 */
let dubbleStrRE = /[^\u4E00-\u9FA5]/g

export function getLocalLen (str) {
  return _.isEmpty(str) ? 0 : str.replace(dubbleStrRE, '**').length
}

export function substrLen (str, len, fix) {
  return (typeof str === 'string' && getLocalLen(str) > len) ? (str.substring(0, len) + (fix !== undefined ? fix : '...')) : str
}

export function getUrlParam (url, name) {
  console.log(url)
  let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
  let r = url.split('?')[1].match(reg)
  if (r != null) return decodeURIComponent(r[2])
  return null
}

export function concatList (arr1, arr2) {
  let arr3 = arr1.concat(arr2), // 合并成一个数组
    temp = {}, // 用于id判断重复
    result = [] // 最后的新数组
  // 遍历c数组，将每个item.id在temp中是否存在值做判断，如不存在则对应的item赋值给新数组，并将temp中item.id对应的key赋值，下次对相同值做判断时便不会走此分支，达到判断重复值的目的
  arr3.map((item, index) => {
    if (!temp[item.id]) {
      result.push(item)
      temp[item.id] = true
    }
  })
  console.log(result)
  return result
}

export const log = (() => {
  if (process.env.NODE_ENV === 'development') {
    return console.log
  }
  return () => {}
})()

export const getToken = () => {
  return localStorage.getItem('real_name') // 此处使用用户名作为token
}
